﻿using NotifyAppBase;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KeyBoardRecordDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Init();
        }


        public void Init()
        {
            var keyItems = Enum.GetValues<Key>();
            ItemsSource = new ObservableCollection<KeyItemViewModel>(keyItems.Select(x => new KeyItemViewModel() { key = x }));
        }


        public ObservableCollection<KeyItemViewModel> ItemsSource
        {
            get { return (ObservableCollection<KeyItemViewModel>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<KeyItemViewModel>), typeof(MainWindow), new PropertyMetadata(null));




        public class KeyItemViewModel : ViewModelBase
        {
            private Key _key;

            public Key key { get => _key; set => Set(ref _key, value); }

            private double _Light;

            public double Light { get => _Light; set => Set(ref _Light, value); }


            private bool _Pressed;

            public bool Pressed { get => _Pressed; set => Set(ref _Pressed, value); }

        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            var keyItem = ItemsSource.FirstOrDefault(x => x.key == e.Key);
            if (keyItem != null)
            {
                keyItem.Pressed = false;

                keyItem.Pressed = true;
            }
        }

        private void Grid_KeyUp(object sender, KeyEventArgs e)
        {
            var keyItem = ItemsSource.FirstOrDefault(x => x.key == e.Key);
            if (keyItem != null)
            {
                Task.Run(async () =>
                {
                    await Task.Delay(TimeSpan.FromSeconds(1));
                    keyItem.Pressed = false;
                });
            }
        }
    }
}
